# _*_ coding: utf8
'''
author: ning.luo
create_date: 2019/03/04
last_update: 2019/03/07
'''

import requests
import unittest
import json
import random

class Cass_quality_assure(unittest.TestCase):
    '''开思质保政策配置相关接口'''

    def setUp(self):
        self.headers = {"Content-Type": "application/json", "charset": "UTF-8"}
        self.base_url = "http://test-api.intra.casstime.com/salepolicyv2-service"

    def add_or_update_cass_assure(self, quality_params, stand_name, time_param):
        '''新增或者修改开始质保配置'''
        url = self.base_url + "/cassassure/create"
        params = {
            "qualityStandNameConfigDTOS": [
                {
                    "cassAssureStandNames": stand_name,
                    "qualityDTOList": quality_params
                }
            ],
            "qualityTime": time_param,
            "userLoginId": "ksmgg"
        }
        print("新增参数：", params)
        response = requests.post(url, data=json.dumps(params), headers=self.headers)
        j = json.loads(response.text)
        try:
            self.assertEqual(response.status_code, 200)
        except Exception as e:
            print("新增Http状态码：", response.status_code)
            print("返回结果：", j)
            print("失败原因：", e)

    def inquire_cass_assure_configuration(self, quality_params,time_param, stand_name):
        '''查询开思质保配置'''
        url = self.base_url + "/list/cassassure/query"
        params = {
            "quality": quality_params,
            "qualityTime": time_param,
            "standName": stand_name
        }
        print("入参：", params)
        response = requests.post(url, data=json.dumps(params), headers=self.headers)
        j = json.loads(response.text)
        try:
            self.assertEqual(response.status_code, 200)
            self.assertEqual(quality_params, j[0]["qualityStandNameConfigDTOS"][0]["qualityDTOList"][0]["qualityType"])
            self.assertEqual(time_param, j[0]["qualityTime"])
            self.assertEqual(stand_name, j[0]["qualityStandNameConfigDTOS"][0]["cassAssureStandNames"][0]["standName"])
        except Exception as e:
            print("查询Http状态码：", response.status_code)
            print("返回结果：", j)
            print("失败原因：", e)
        print("----------------分隔符------------------")

    def delete_policy(self, time_param):
        '''删除开思质保政策'''
        url = self.base_url + "/cassassure/delete/" + time_param
        response = requests.delete(url)
        self.assertEqual(200, response.status_code)
        print("删除成功！")


    def test01(self):
        '''主流程测试'''
        for i in range(1):
            quality_list = [{"qualityType":"ORIGINAL_BRAND"}, {"qualityType":"EXTERNAL_BRAND"}, {"qualityType":"INTERNAL_BRAND"},
                            {"qualityType":"OTHER_BRAND"}, {"qualityType":"SECOND_HAND"}, {"qualityType":"ORIGINAL_OTHERS"},
                            {"qualityType":"ORIGINAL_CURRENCY"}]
            quality_num = random.randint(1, 7)
            quality_list1 = random.sample(quality_list, quality_num)

            quality_time = ["QUALITY_ASSURE_1MONTH", "QUALITY_ASSURE_3MONTH", "QUALITY_ASSURE_6MONTH",
                            "QUALITY_ASSURE_1YEAR",
                            "QUALITY_ASSURE_2YEAR"]
            time_param = random.choice(quality_time)

            stand_name_list = [{"standName":"火花塞"}, {"standName":"叶子板"}, {"standName":"气门芯"}, {"standName":"节气门"},
                               {"standName":"PVC阀盖"}, {"standName":"进气管支架"}, {"standName":"空气流量计胶圈"}, {"standName":"空气管"},
                               {"standName":"大灯喷水盖"}, {"standName":"大灯喷水嘴"}, {"standName":"方向盘固定支架"}, {"standName":"副邦肽"},
                               {"standName":"油箱盖座"}, {"standName":"油箱隔热罩"}, {"standName":"冷凝器"}, {"standName":"冷凝器支架"},
                               {"standName":"车壳"}, {"standName":"挡泥板支架"}]
            stand_name_num = random.randint(1, 18)
            stand_name = random.sample(stand_name_list, stand_name_num)

            self.add_or_update_cass_assure(quality_list1, stand_name, time_param)
            for j in range(len(stand_name_list)-1):
                for k in range(len(quality_list1)-1):
                    self.inquire_cass_assure_configuration(quality_list1[k]["qualityType"], time_param,
                                                           stand_name_list[j]["standName"])
            self.delete_policy(time_param)
            print("---------------------请求%d-----------------"%(i+1))

    def test02(self):
        '''验重校验'''


    def tearDown(self):
        self.headers = None
        self.base_url = None

if __name__ == "__mian__":
    unittest.main()